package ProjectViewer.Window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

public class Window_MemoryMapView extends JInternalFrame {

	private static final long serialVersionUID = 3798315477025824088L;

	private MemoryDisplay memoryDisplay;
	
	public Window_MemoryMapView() {
		super("MemoryMap");
		setSize(400,400);
		
		memoryDisplay = new MemoryDisplay(0x80040000);
		getContentPane().add(memoryDisplay,    BorderLayout.CENTER);
		getContentPane().add(new TopToolBar(), BorderLayout.NORTH);
		
		show();
	}
	
	class TopToolBar extends JPanel {
		
		private static final long serialVersionUID = 1825276987969331978L;
		
		public TopToolBar() {
			super();
			
			JButton b = new JButton("<<");
			b.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					memoryDisplay.stepBackward(8);
				}
			});
			add(b);
			
			b = new JButton(">>");
			b.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					memoryDisplay.stepForward(8);
				}
			});
			add(b);
			
		}
		
	}
	
	class MemoryDisplay extends JPanel{

		private static final long serialVersionUID = -2094669956259076707L;

		private int baseAddress;
		private int currentAddress;
		private int step;
		boolean draw_fieldData, draw_methodData, draw_classData;
		
		public MemoryDisplay(int baseAddress) {
			super();
			
			this.baseAddress    = baseAddress;
			this.currentAddress = baseAddress;
			this.step = 8;
			
			setLayout(null);
			setSize(400, 400);
			setPreferredSize(new Dimension(400,400));
			setBackground(Color.white);
		}
		
		@Override
		public void paint(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			super.paint(g2);
			
			drawAddress(g2);
			drawClassData(g2);
			drawFieldData(g2);
			drawMethodData(g2);
			
		}
		
		private void drawAddress(Graphics2D g) {
			g.drawLine(0, 20, getWidth(), 20);
			g.drawString("0x" + String.format("%x", currentAddress),        10,  50);
			g.drawLine(0, 70, getWidth(), 70);
			
			g.drawString("0x" + String.format("%x", currentAddress+1*step), 10, 100);
			g.drawLine(0, 120, getWidth(), 120);
			
			g.drawString("0x" + String.format("%x", currentAddress+2*step), 10, 150);
			g.drawLine(0, 170, getWidth(), 170);
			
			g.drawString("0x" + String.format("%x", currentAddress+3*step), 10, 200);
			g.drawLine(0, 220, getWidth(), 220);
		}
		
		private void drawClassData(Graphics2D g) {
			if (draw_classData) {
				
			}
		}
		
		private void drawFieldData(Graphics2D g) {
			if (draw_fieldData) {
				
			}
		}
		
		private void drawMethodData(Graphics2D g) {
			if (draw_methodData) {
				
			}
		}
		
		public void stepForward(int value) {
			currentAddress += value;
			lookup();
			repaint();
		}
		
		public void stepBackward(int value) {
			if (currentAddress > baseAddress) {
			  currentAddress -= value;
			  lookup();
			  repaint();
			}
		}
		
		/**
		 * 
		 */
		private void lookup() {
			
		}
		
	}
	
}
